import re
import requests
class Douban():
    def __init__(self):#初始化
        self.headers={"User-Agent":'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.'
                                   '36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 OPR/80.0.4170.63'}#伪装请求头

        self.res=requests.get('https://movie.douban.com/top250',headers=self.headers)#获取网址数据
        self.get=self.res.content.decode('utf-8')#获取相应内容并解码
    def GetImage(self):   #正则处理获取的响应数据
        self.RP = re.findall(r'<img width="100" alt="(.*)" src=".*" class="">', self.get)
        self.RE = re.findall(r'<img width="100" alt=".*" src="(.*)" class="">', self.get)
        return self.RP
    def SaveImage(self):#保存数据
        flag = 0
        for i in self.RE:
            print(i)
            res = requests.get(i)
            res_get = res.content
            with open("image/" + self.RP[flag] + '.png', 'wb') as image:
                image.write(res_get)

            flag += 1
    def run(self):#运行代码
        self.GetImage()
        self.SaveImage()
if __name__ == '__main__':
    a=Douban()
    a.run()

# headers={"User-Agent":'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 OPR/80.0.4170.63'}
#
# res=requests.get('https://movie.douban.com/top250',headers=headers)
# text_get=res.content.decode('utf-8')
# RP=re.findall(r'<img width="100" alt="(.*)" src=".*" class="">',text_get)
# RE=re.findall(r'<img width="100" alt=".*" src="(.*)" class="">',text_get)
#
# print(RP)
#
# flag = 0
# for i in RE:
#     print(i)
#
#     ress=requests.get(i)
#     ress_get=ress.content
#
#
#     with open("image/"+RP[flag]+'.png','wb') as image:
#         image.write(ress_get)
#
#     flag += 1











#
# import requests
# import re
#
# res = requests.get("https://read.qidian.com/chapter/SaJ9L6aSg4E1/RVkDZcwLjh4ex0RJOkJclQ2/")
#
# str1 = res.content.decode()
#
# ret = re.findall('<div class="read-content j_readContent" id="">.*?</div>', str1, re.S)
# for i in ret:
#     new_str = i.split("<p>")
#     # print(new_str)
# # 此刻列表当中包含了切分之后的文本数据
# text_list = new_str[1:-2]
# with open("zhetian.txt", "w") as f:
#     for x in text_list:
#         new_str = x.strip()
#         f.write(new_str + "\n")